
#include <GL/glew.h>
#include <SDL/SDL.h>


#include <iostream>

#include "MntFile.h"
#include "Displayer.h"
#include "MntDisplayer.h"
#include "readpng.h"
#include "Mesh.h"
#include "OffFile.h"
#include "Camera.h"
#include "Vector3F.h"
#include "recordGL.h"

using namespace std;

#define DBG cout<<__LINE__<<endl;

gl_texture_t *LEA;
gl_texture_t *LEO;

recordGL myrec("film/",640 , 480);

void selectionner (Objet3D *o, Camera * camera)
{
	Vector3F t = camera->getCible();
	float  f [3] = {t.X, t.Y, t.Z-10.0};
	o->translate(f);
}

void initPhoto()
{
	glEnable(GL_TEXTURE_2D);
	LEA = ReadPNGFromFile("ressources/lea.png");
	glGenTextures(1, &LEA->id);
	glBindTexture(GL_TEXTURE_2D, LEA->id);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexImage2D (GL_TEXTURE_2D, 0,  
	LEA->internalFormat,  LEA->width,  
	LEA->height, 0,  LEA->format, 
	GL_UNSIGNED_BYTE,  LEA->texels);
	
	LEO = ReadPNGFromFile("ressources/leo.png");
	glGenTextures(1, &LEO->id);
	glBindTexture(GL_TEXTURE_2D, LEO->id);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexImage2D (GL_TEXTURE_2D, 0,  
	LEO->internalFormat,  LEO->width,  
	LEO->height, 0,  LEO->format, 
	GL_UNSIGNED_BYTE,  LEO->texels);

}

void photo()
{
	glEnable(GL_TEXTURE_2D);
	
	glBindTexture(GL_TEXTURE_2D, LEA->id);
	glBegin(GL_QUADS);
		
		glTexCoord2d(0.0,1.0);
		glVertex3f(0.0, 0.0, 150.0);
		glTexCoord2d(0.0,0.0);
		glVertex3f(0.0, 0.0, 125.0);
		glTexCoord2d(1.0,0.0);
		glVertex3f(0.0, 25.0, 125.0);
		glTexCoord2d(1.0,1.0);
		glVertex3f(0.0, 25.0, 150.0);
	glEnd();
	
	glBindTexture(GL_TEXTURE_2D, LEO->id);
	glBegin(GL_QUADS);
		
		glTexCoord2d(0.0,1.0);
		glVertex3f(0.0, 26.0, 150.0);
		glTexCoord2d(0.0,0.0);
		glVertex3f(0.0, 26.0, 125.0);
		glTexCoord2d(1.0,0.0);
		glVertex3f(0.0, 51.0, 125.0);
		glTexCoord2d(1.0,1.0);
		glVertex3f(0.0, 51.0, 150.0);
	glEnd();
}



void Dessiner(Displayer * d, Mesh * mesh, Camera * camera)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glMatrixMode(GL_MODELVIEW);
    
    
    
    glLoadIdentity();
    camera->look();
    
    photo();
    d->display();
    //glBindTexture(GL_TEXTURE_2D, 0);
    //parcourir le mesh
    for(unsigned int i = 0; i < mesh->size(); i++)
    {
		glColor3ub(255,0,0);
		(*mesh)[i]->display();
	}
    glColor3ub(255,255,255);
    glFlush();
    myrec.write_ppmfile();
    SDL_GL_SwapBuffers();
}


int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("Mouillere en 3D \\m/",NULL);
    SDL_SetVideoMode(640,480, 32, SDL_OPENGL);

	Uint32 current_time, elapsed_time, last_time = 0;

	if(glewInit() != GLEW_OK)
	{
		cerr << "Initialisation des extensions impossible" << endl;
		return -1;
	}


    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective(70, (double)640/480, 0.1, 100000);
    glEnable(GL_DEPTH_TEST);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnable(GL_TEXTURE_2D);
    
    Uint8 *keystate;
    bool selection [3] = {false, false, false};
    
    initPhoto();
    
    Vector3F vct(0,0,300);
    
	Camera camera(vct);
    
    MntFile m("ressources/mouillere2.mnt", "ressources/mouillere2.png");
    Displayer *d = new MntDisplayer(&m);
    Mesh mesh;
    
    //ajouter les maillages off ici plus coordonnées
    OffFile f1("ressources/x29_plane.off");
    OffFile f2("ressources/klingon2.off");
    OffFile f3("ressources/space_station2.off");
    
    float pos1[3] = {300.0, 300.0, 150.0};
    float pos2[3] = {100.0, 100.0, 150.0};
    float pos3[3] = {200.0, 200.0, 150.0};
    
    Objet3D o1(&f1);
    Objet3D o2(&f2);
    Objet3D o3(&f3);
    
    mesh.add(&o1, pos1);
    mesh.add(&o2, pos2);
    mesh.add(&o3, pos3);
    
    //Dessiner(d, &mesh, &camera);
    bool continuer = true;
    SDL_Event event;
   // SDL_EnableKeyRepeat(15,69);
   myrec.switch_recording("test");
    while (continuer)
        {
            SDL_PollEvent(&event);
            switch(event.type)
                {
                case SDL_QUIT:
                    continuer = false;
                case SDL_MOUSEMOTION          : camera.mvtSouris(); break;
                case SDL_MOUSEBUTTONUP        : camera.moletteSouris(event.button); break;
                case SDL_MOUSEBUTTONDOWN      : camera.moletteSouris(event.button); break;
                }
          
            keystate = SDL_GetKeyState(NULL);
            
            if(keystate[SDLK_f]) //mode fil de fer
				glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
				
			if(keystate[SDLK_p]) //mode plein
				glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
			
			if(keystate[SDLK_ESCAPE]) //quitter
				continuer = false;
				
			if(keystate[SDLK_a]) //selectionner l'avion
			{
				if(!selection[0])
					selection[0] = true;
			}
			
			if(keystate[SDLK_k]) //selectionner l'avion
			{
				if(!selection[1])
					selection[1] = true;
			}
			
			if(keystate[SDLK_n]) //selectionner l'avion
			{
				if(!selection[2])
					selection[2] = true;
			}
			
			if(keystate[SDLK_c]) //annule les séléction
			{
				selection[0] = selection[1] = selection[2] = false;
			}
				
			camera.clavier(keystate);
			
			current_time = SDL_GetTicks();
			elapsed_time = current_time - last_time;
			last_time = current_time;

			camera.animer(elapsed_time);
			
			if(selection[0])
				selectionner(&o1, &camera);
			
			if(selection[1])
				selectionner(&o2, &camera);
				
			if(selection[2])
				selectionner(&o3, &camera);
			
            Dessiner(d, &mesh, &camera);

        }

	myrec.switch_recording("test");
	myrec.treat_recording();
    SDL_Quit();


    return 0;
}










